﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace AdminSystem.Repositories
{
    public class Repositories<TEntity>:IRepositories<TEntity> where TEntity : class
    {
        /// <summary>
        /// 上下文
        /// </summary>
       private readonly AdminSystemSCDBContext db;

        public Repositories(AdminSystemSCDBContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> AddRange(IEnumerable<TEntity> entities)
        {
            db.Set<TEntity>().AddRange(entities);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> Delete(int id)
        {
            db.Set<TEntity>().Remove(db.Set<TEntity>().Find(id)); 
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> DeleteRange(IEnumerable<int> ids)
        {
            db.Set<TEntity>().RemoveRange(db.Set<TEntity>().Find(ids));
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 根据条件是否存在
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await db.Set<TEntity>().AnyAsync(predicate);
        }
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        public async Task<IList<TEntity>> GetAll()
        {
            return await db.Set<TEntity>().ToListAsync();
        }
        /// <summary>
        /// 排序
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderby"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<TEntity>> GetAll<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> orderby)
        {
            return await db.Set<TEntity>().Where(predicate).OrderBy(orderby).ToListAsync();
        }
        /// <summary>
        /// 查询一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<TEntity> GetById(int id)
        {
            return await db.Set<TEntity>().FindAsync(id);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Insert(TEntity entity)
        {
            db.Set<TEntity>().Add(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 根据条件查询多条
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await db.Set<TEntity>().Where(predicate).ToListAsync();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Update(TEntity entity)
        {
            db.Set<TEntity>().Update(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 修改批改
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateRange(IEnumerable<TEntity> entities)
        {
            db.Set<TEntity>().UpdateRange(entities);
            return await db.SaveChangesAsync();
        }
    }
}
